<?php
    require_once('paths.inc');
    require_once(INCLUDE_PATH.'constants.inc');
    require_once(DB_PATH.'db.inc');
    //incarca dependintele
    require_once 'utils.inc';
    require_once INCLUDE_PATH . 'classes/Address.class.php';
    require_once INCLUDE_PATH . 'classes/Comanda.class.php';
    require_once INCLUDE_PATH . 'classes/PlatiComanda.class.php';
    require_once INCLUDE_PATH . 'classes/cipher_blowfish.php';
    
    $codedIdClient = urldecode($_GET['id']);
    $nrReferintaSol = $_GET['id2'];
    
    $coder = new Horde_Cipher_blowfish();
    $coder->setKey("scumpascumpelor");

    $decodedIdClient = trim($coder->decryptBlock($codedIdClient));
    
    $db = db_connect();
    if (filter_var($nrReferintaSol, FILTER_VALIDATE_INT) === FALSE)
        die();

    $comanda = Comanda::queryLoadByIdClientAndNrReferintaSol($db, $decodedIdClient, $nrReferintaSol);
    
    ob_start(); 
    ?>
    
    <table class="ol_table">
    
    <?php if (!empty($comanda->status)) { ?>
        <tr>
            <td colspan="3" align="center" class="ol_table_status">Statusul comenzii: <?php echo $comanda->status; ?></td>
        </tr>
    <?php } ?>
        
    <tr>
        <td colspan="3" align="center" class="ol_table_list">Lista locurilor unde s-au efectuat incarcari</td>
    </tr>
    
    <?php
    $flag = 1;
    $nr = count($comanda->adrese_incarcare);
    for ($i = 0; $i < $nr; $i++) {
        if (!(empty($comanda->adrese_incarcare[$i]->data_efectiva))) {
            if ($flag != 2) { 
                $flag = 2;
                ?>
                <tr align="center" class="ol_table_date">
                    <td>Adresa locului</td>
                    <td>Data programata</td>
                    <td>Data realizata</td>
                </tr>
            <?php }
                        
            $adresa = $comanda->adrese_incarcare[$i];
            $loc = $adresa->adresa . " " . $adresa->oras . " " . " " . $adresa->cod_postal . " " . $adresa->tara; ?>

            <tr class="ol_table_row">
                <td width="200" align="center"><?php echo $loc; ?></td>
                <td><?php echo getDateForUser($adresa->data, true); ?></td>
                <td><?php echo getDateForUser($adresa->data_efectiva, true); ?></td>
            </tr>
        <?php }
    }
    
    if ($flag == 1) { ?>
        <tr align="center" class="ol_table_date">
            <td colspan="3"><i>Nu exista inca locuri unde s-au efectuat incarcari</i></td>
        </tr>    
    <?php }
    else $flag = 1;
    ?>
    <tr>
        <td colspan="3" align="center" class="ol_table_list">Lista locurilor unde urmeaza sa se faca incarcari</td>
    </tr>
    <?php
    for ($i = 0; $i < $nr; $i++) {
        if (empty($comanda->adrese_incarcare[$i]->data_efectiva)) {
            if ($flag != 2) {
                $flag = 2;
                ?>
                <tr align="center" class="ol_table_date">
                    <td>Adresa locului</td>
                    <td align="center" colspan="2">Data programata</td>
                </tr>
            <?php }

            $adresa = $comanda->adrese_incarcare[$i];
            $loc = $adresa->adresa . " " . $adresa->oras . " " . " " . $adresa->cod_postal . " " . $adresa->tara; ?>
            
            <tr class="ol_table_row" align="center">
                <td width="200" ><?php echo $loc; ?></td>
                <td colspan="2"><?php echo getDateForUser($adresa->data, true); ?></td>
            </tr>
        <?php }
    }
    if ($flag == 1) { ?>
        <tr align="center" class="ol_table_date">
            <td colspan="3"><i>Nu exista inca locuri unde urmeaza sa se faca incarcari</i></td>
        </tr>
    <?php }
    else $flag = 1;
    ?>
    
    <tr>
        <td colspan="3" align="center" class="ol_table_list">Lista locurilor unde s-au facut descarcari</td>
    </tr>
    
    <?php
    $nr = count($comanda->adrese_descarcare);
    for ($i = 0; $i < $nr; $i++) {
        if (!(empty($comanda->adrese_descarcare[$i]->data_efectiva))) {
            if ($flag != 2) {
                $flag = 2;
                ?>
                <tr align="center" class="ol_table_date">
                    <td>Adresa locului</td>
                    <td>Data programata</td>
                    <td>Data realizata</td>
                </tr>
            <?php }

            $adresa = $comanda->adrese_descarcare[$i];
            $loc = $adresa->adresa . " " . $adresa->oras . " " . " " . $adresa->cod_postal . " " . $adresa->tara; ?>
            
            <tr class="ol_table_row">
                <td width="200" align="center"><?php echo $loc; ?></td>
                <td><?php echo getDateForUser($adresa->data, true); ?></td>
                <td><?php echo getDateForUser($adresa->data_efectiva, true); ?></td>
            </tr>
        <?php }
    }

    if ($flag == 1) { ?>
        <tr align="center" class="ol_table_date">
            <td colspan="3"><i>Nu exista inca locuri unde s-au realizat descarcari</i></td>
        </tr>
    <?php }
    else $flag = 1;
    ?>

    <tr>
        <td colspan="3" align="center" class="ol_table_list">Lista locurilor unde urmeaza sa se faca descarcari</td>
    </tr>
    <?php
    for ($i = 0; $i < $nr; $i++) {
        if (empty($comanda->adrese_descarcare[$i]->data_efectiva)) {
            $adresa = $comanda->adrese_descarcare[$i];
            $loc = $adresa->adresa . " " . $adresa->oras . " " . " " . $adresa->cod_postal . " " . $adresa->tara;
            
            if ($flag != 2) { 
                $flag = 2;  ?>
                <tr align="center" class="ol_table_date">
                    <td>Adresa locului</td>
                    <td align="center" colspan="2">Data programata</td>
                </tr>    
            <?php } ?>
            
            <tr class="ol_table_row" align="center">
                <td width="200" ><?php echo $loc; ?></td>
                <td colspan="2"><?php echo getDateForUser($adresa->data, true); ?></td>
            </tr>
        <?php }
    }
    if ($flag == 1) { ?>
        <tr align="center" class="ol_table_date">
            <td colspan="3"><i>Nu exista inca locuri unde s-au efectuat descarcari</i></td>
        </tr>    
    <?php } 
    else $flag = 1;
    ?>

    </table>
    
    <?php 
        $response = ob_get_clean();
        //make output reliable for javascript -- start
        $response = str_replace("\n", "", $response);
        $response = str_replace("\r", "", $response);
        //make output reliable for javascript -- end
        echo "show_result('" . $response . "');";
    ?>